#include <iostream>
#include <string>
#include <vector>

#include <kylin-ai/ai-base/datamanagement.h>
#include <jsoncpp/json/json.h>

#include "knowledgebaseprocessorglue.h"



void test_sdk();
void test_add_text_data();
void test_add_image_data();
void test_delete();
void test_get_all_files();


int main(int argc, char* argv[]) {

    //test

    // 往数据库中加入内容
    // DataManagementSession session = nullptr;
    // data_management_create_session(&session, SYS_SEARCH, 1000);

    // add前检查文件格式
    // char *result = nullptr;
    // DataManagementResult res = data_management_check_file_format(session,"/home/ukylin/demo/test/常见问题.pdf",&result);
    // std::cout<<res<<" "<< result <<std::endl;
    // test_add_text_data();
    //test_add_image_data();

    //data_management_free_check_file_format_result(session,result);


    //data_management_get_feature_status
    // FeatureStatus status = FeatureStatus::UNKNOWN;
    // DataManagementResult result = data_management_get_feature_status(session, &status);
    // if (result == DataManagementResult::DATA_MANAGEMENT_SUCCESS &&
    //     status == FeatureStatus::AVAILABLE) {
    //     std::cout << " result: "<< result <<std::endl;
    // }



    //test_get_all_files();

    //搜索
    test_sdk();
    //test_delete();

    std::cout << " Hello World " << std::endl;

    // DataManagementResult res = data_management_destroy_session(session);
    // std::cout<<res<<std::endl;
    return 0;
}

std::string getFileExtension(const std::string& filename) {
    std::size_t dotPosition = filename.find_last_of(".");

    if (dotPosition == std::string::npos || dotPosition == filename.length() - 1) {
        return "";
    }   

    return filename.substr(dotPosition + 1);
}



void test_sdk() {
    DataManagementSession session = nullptr;
    data_management_create_session(&session, SYS_SEARCH, 1000);

    
    char* result = nullptr;
    std::string conditions = R"({"text": "search"})";
    DataManagementResult res = data_management_similarity_search(session, conditions.c_str(), &result);

    std::cout << result << " " << res << std::endl;

    // char* checkResult = nullptr;
    // std::string filePath = "/home/ukylin/demo/test";
    // DataManagementResult res1 = data_management_check_file_format(
    //     session, filePath.c_str(), &checkResult);

    // if (!checkResult)
    //     std::cout << res1 << std::endl;
    // else
    //     std::cout << checkResult << " " << res1 << std::endl;

}

void test_add_text_data()
{
    DataManagementSession session = nullptr;
    data_management_create_session(&session, SYS_SEARCH, 1000);

    std::string json = R"(
    [
        {
            "filepath": "/home/ukylin/demo/test/常见问题.pdf",
            "fileformat": "pdf"
        }
    ])";

    data_management_add_text_files(session, json.c_str());
}

void test_add_image_data() {
    DataManagementSession session = nullptr;
    data_management_create_session(&session, SYS_SEARCH, 1000);

    std::string json = R"(
    [
        {
            "filepath": "/home/ukylin/demo/test/qwer.jpg",
            "fileformat": "jpg"
        },
        {
            "filepath": "/home/ukylin/demo/test/asdf.jpg",
            "fileformat": "jpg"
        },
    ])";
    data_management_add_image_files(session, json.c_str());
}

void test_delete() {
    DataManagementSession session = nullptr;
    data_management_create_session(&session, SYS_SEARCH, 1000);
    std::string json = R"(
    [
        {
            "filepath": "/home/ukylin/demo/test/qwer.jpg"
        },
        {
            "filepath": "/home/ukylin/demo/test/animal.txt"
        }
    ])";

    DataManagementResult res = data_management_delete_files(session, json.c_str());
    std::cout << "delete result: " << res << std::endl;
}

void test_get_all_files()
{
    std::cout << "test get all files" << std::endl;
    DataManagementSession session = nullptr;
    data_management_create_session(&session, SYS_SEARCH, 1000);
    char* result = nullptr;
    DataManagementResult res = data_management_get_all_fileinfos(session, &result);
    std::cout << res << " " << result << std::endl;
}